package com.example.demo05.controller;

import com.example.demo05.pojo.ClockTable;
import com.example.demo05.pojo.User;
import com.example.demo05.service.ClockService;
import com.example.demo05.service.UserService;
import com.example.demo05.util.DateTimeUtil;
import com.example.demo05.util.ExportExcelUtils;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;


/**
 * @author created by Zhangdazhuang
 * @version v.0.1
 * @Description TODO
 * @date 2019/4/7
 * @备注
 **/
@Controller
@Slf4j
@RequestMapping(value = "/excel")
public class ExcelController {


    @Autowired
    private ClockService clockService;

    @Autowired
    private UserService userService;

    @GetMapping(value = "/e/{date}")
    public void ExportBankCkeckInfo(HttpServletResponse response, HttpServletRequest request, @PathVariable String date){


        //这里是笔者实际业务需求中需要得到时间间隔。可忽略
        String start=request.getParameter("start");
        String end=request.getParameter("end");
        System.out.println("打印的起始日期为："+start+"，打印的结束日期为："+end);
        //得到所有要导出的数据
        List<ClockTable> list = clockService.selectAll();
        List<ClockTable> orderlist = new ArrayList<>();

        for(int i=0;i<list.size();i++){
            list.get(i).setClockTimeStr(DateTimeUtil.dateToStr(list.get(i).getClockTime(),"yyyy-MM-dd"));
            if(Objects.equals(list.get(i).getClockTimeStr(), date)) {
                orderlist.add(list.get(i));
            }
        }
        //定义导出的excel名字
        String excelName = date+" 打卡信息";

        //获取需要转出的excel表头的map字段
        LinkedHashMap<String, String> fieldMap = new LinkedHashMap<>();
        fieldMap.put("loginId","学号");
        fieldMap.put("username","姓名");
        fieldMap.put("identityNumber","身份证号");
        fieldMap.put("phone","手机号");
        fieldMap.put("clockTimeStr","打卡日期");
        fieldMap.put("temperature","体温");
        fieldMap.put("state","状态");




        //导出用户相关信息
        new ExportExcelUtils().export(excelName,orderlist,fieldMap,response);
    }



    @GetMapping(value = "/ee")
    public void ExportBankCkeckInfo1(HttpServletResponse response, HttpServletRequest request){


      //  log.info("进入excelllllllllll");

        //这里是笔者实际业务需求中需要得到时间间隔。可忽略
        String start=request.getParameter("start");
        String end=request.getParameter("end");
        System.out.println("打印的起始日期为："+start+"，打印的结束日期为："+end);
        //得到所有要导出的数据
        List<User> orderlist = userService.getNoClock();
       // log.info("111111111111111,{}",orderlist);

        //定义导出的excel名字
        String excelName = "今日未打卡学生";

        //获取需要转出的excel表头的map字段
        LinkedHashMap<String, String> fieldMap = new LinkedHashMap<>();
        fieldMap.put("loginid","学号");
        fieldMap.put("username","姓名");
        fieldMap.put("identitynumber","身份证号");
        fieldMap.put("phone","手机号");






        //导出用户相关信息
        new ExportExcelUtils().export(excelName,orderlist,fieldMap,response);
    }



}
